<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <!--    导入自己的css文件-->
    <link rel="stylesheet" href="../css/my-css.css">
    <!--引入富文本编辑器css-->
    <link href="https://cdn.staticfile.org/wangeditor5/5.1.23/css/style.min.css" rel="stylesheet">

    <style>
        .center{
            width: 1200px;
            margin: 0 auto;
        }
        #footer_center p{
            margin: 0;
        }
        #footer_center h3{
            color: white;
        }
        body{
            background-color: rgb(241,242,243);
        }
        .el-header{
            background-color: white;
        }
        p{
            margin-bottom: 30px;
        }
    </style>
</head>
<body>
<div id="app" >
    <el-container>
        <my-header></my-header>
        <el-main class="center" style="width: 1200px; height: 600px; margin: 0 auto;padding-left: 125px">
            <el-row gutter="20">
                <el-col span="4">

                    <el-card style="height: 800px">
                        <p @click="location.href='../usermodel/personal.html'" style="cursor: pointer">个人信息</p>
                        <p @click="location.href='../usermodel/friendlist.html'"  style="cursor: pointer">好友列表</p>
                        <p style="color: #409EFF">笔记管理</p>
                        <p @click="location.href='../videomodel/postVideo.html'" style="cursor: pointer">视频管理</p>
                    </el-card>
                </el-col>
                <el-col span="20">
                    <el-card style="height: 1200px">
                        <!--发布文章表单开始-->
                        <el-form label-width="100px">
                            <el-form-item label="笔记标题">
                                <el-input style="width: 200px" v-model="c.title"
                                          placeholder="请输入标题"></el-input>
                            </el-form-item>
                            <el-form-item label="笔记分类">
                                <el-select v-model="c.subjectId">
                                    <el-option v-for="cat in subjectArr"
                                               :label="cat.name" :value="cat.id"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="封面">
                                <img v-if="isEdit" :src="oldCover"
                                     style="width: 145px;height: 145px;">
                                <el-upload
                                        action="/v1/upload"
                                        name="file"
                                        limit="1"
                                        list-type="picture-card"
                                        :on-success="handleSuccess"
                                        :on-preview="handlePictureCardPreview"
                                        :on-remove="handleRemove">
                                    <i class="el-icon-plus"></i>
                                </el-upload>
                                <el-dialog :visible.sync="dialogVisible">
                                    <img width="100%" :src="dialogImageUrl" alt="">
                                </el-dialog>
                            </el-form-item>

                            <el-form-item label="笔记内容">
                                <div style="border: 1px solid #ccc;height: 600px">
                                    <div id="toolbar-container" style="border-bottom: 1px solid #ccc;"><!-- 工具栏 --></div>
                                    <div id="editor-container" style="height: 300px;"><!-- 编辑器 --></div>
                                </div>
                            </el-form-item>
                            <el-form-item style="float: left">
                                <el-button type="primary" @click="post()">{{isEdit?"修改":"发布"}}</el-button>
                            </el-form-item>

                            <el-form-item style="float: right">
                                <el-button type="primary" @click="location.href='../notemodel/noteManagement.html'">返回</el-button>
                            </el-form-item>
                        </el-form>

                        <!--发布文章表单结束-->

                    </el-card>
                </el-col>
            </el-row>
        </el-main>
        <my-footer></my-footer>
    </el-container>
</div>
</body>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<!--引入富文本编辑器js-->
<script src="https://cdn.staticfile.org/wangeditor5/5.1.23/index.min.js"></script>
<!--引入Axios-->
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
<!--导入自己的头部js文件-->
<script src="../js/my-header.js"></script>
<!--导入自己的尾部js文件-->
<script src="../js/my-footer.js"></script>

<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                oldCover:'',
                isEdit:false,
                dialogImageUrl: '',
                dialogVisible: false,
                subjectArr:[],
                c:{
                    title:"", cover:"",content:"",subjectId:"",
                },
            }
        },
        methods: {
            post(){
                if (v.c.cover==""){
                    v.$message.error("请选择一张封面图!");
                    return;
                }
                if (v.c.subjectId==null||v.c.subjectId==""){
                    v.$message.error("请选择分类!");
                    return;
                }
                //设置作者id
                v.c.createBy = JSON.parse(localStorage.user).id;
                //发出请求
                axios.post("/v1/notes/insert",v.c).then(function (response) {
                    if (response.data.code==20000){
                        v.$message.success("发布完成!");
                        location.href="/notemodel/noteManagement.html";
                    }
                })
            },
            handleSuccess(response, file, fileList) {
                console.log(file);
                if (file.raw.type.includes("image")){
                    //把上传图片完成之后 得到的图片路径用变量记录
                    v.c.cover = response.data;
                }
            },
            handleRemove(file, fileList) {
                //判断删除的是图片还是视频
                if (file.raw.type.includes("image")){
                    v.c.cover="";
                }
                //发出删除文件的请求   file.response上传成功时服务器响应的内容ResultVO
                console.log(file.response);
                axios.get("/v1/upload/remove?url=" + file.response.data).then(function () {
                    console.log("服务器文件删除完成!");
                })
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            loadCategories(){
                axios.get("/v1/subject/selectSubject").then(function (response) {
                    if (response.data.code==20000){
                        v.subjectArr = response.data.data;
                    }
                })
            }
        },
        created:function () {
            //判断此时页面的作用是添加还是修改
            //location.search得到地址栏中?及后面的内容   ?id=25
            // "abc".includes("a")也是判断是否包含
            if (location.search.indexOf("id")!=-1){//包含id 代表编辑
                this.isEdit = true;
                let id = location.search.split("=")[1];
                //通过地址栏中id请求内容详情
                axios.get("/v1/notes/editById/" + id).then(function (response) {
                    if (response.data.code==20000){
                        v.c = response.data.data;
                        //更新富文本编辑器内容
                        editor.setHtml(v.c.content);
                        //给单独的变量赋值,让页面中的图片标签和oldImgUrl绑定
                        v.oldCover = v.c.cover;
                    }
                })
            }else {//代表发布

            }
            this.loadCategories();

        }
    })

    /**********富文本编辑器相关js开始***********/
    const { createEditor, createToolbar } = window.wangEditor
    const editorConfig = {
        placeholder: '请填写详情...',
        onChange(editor) {
            const html = editor.getHtml();
            console.log('editor content', html);
            //当富文本编辑器里面的值发生改变时 把值赋值给内容对象
            v.c.content = html;
        }
    }
    const editor = createEditor({
        selector: '#editor-container',
        html: '<p><br></p>',
        config: editorConfig,
        mode: 'simple',
    })
    const toolbarConfig = {}
    const toolbar = createToolbar({
        editor,
        selector: '#toolbar-container',
        config: toolbarConfig,
        mode: 'simple',
    })
    /**********富文本编辑器相关js结束***********/
</script>
</html>